<?php
defined('BY_JYA') or exit('error');
if($_SESSION['os']!='windows'){
	cfc('core')->checklogin();
}
if(!$_SESSION['uid']){
	exi('','','/login/');
}
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$setting = plugin('jy_merch')->plugin_setting('setting');
set_html('title','注册商户');
if($_RQ['op']=='get'){
	if(!$_RQ['add']){
		$item=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		if($item){
			if($item['status']==1){
				exi('你已申请无需重复申请','error','jy_merchfuwu/manage.index/');
			}
		}
	}
	if($setting['is_level']){
		$levelid=pdo_getcolumn('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'level');
		if(!$levelid){
			exi('您当前等级不能申请开通！','error');
		}
	}
	if(empty($item)){
		$member = pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','realname','credit2','mobile','groupid','avatar','nickname'));
		$item =array(
			'merchname'=>$member['nickname'],
			'realname'=>$member['realname'],
			'mobile'=>$member['mobile'],
			'logo'=>$member['avatar'],
		);
	}
	$return['set']=$setting;
	$item['lat_lng']=$item['lat_lng']?json_decode($item['lat_lng'],true):'';
	$item['codeimg']=$item['codeimg']?json_decode($item['codeimg'],true):array();
	$item['addressinfo']=array('address'=>$item['address'],'lat_lng'=>$item['lat_lng']);
	$item['zhaoshangid']=intval($_RQ['zhaoshangid']);
	$item['upass']='';
	$item['diyfields']=$setting['diyformfields']?:array();
	$return['cate']=pdo_getall('jy_merch_category',array('uniacid'=>$_SESSION['uniacid']),array('id','catename'));
	foreach($return['cate'] as $k=>&$gv){
		if($item['cateid']==$gv['id']){
			$item['pcateindex']=$k;
		}
	}
	$return['post']=$return['item']=$item;
	$return['set']['regbg']=tomedia($return['set']['regbg']);
	if($_SESSION['wxapp_id']){
		$tmpid=cfc('wxapp_api')->get_tpl_template_id('10415',[5,4,2],'shenhe','审核结果通知','jy_merch');
		if($tmpid){
			$tmplIds[]=$tmpid;
		}
		$return['tmplIds']=$tmplIds;
	}
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if($_SESSION['container']!='wxapp' && $return['basic']['is_smscode'] && !check_sms_code($postdata['smscode'])){
		exi('手机验证码错误！','error');
	}
	if(!$postdata['realname']){
		exi('请填写姓名','error');
	}
	if(!$postdata['mobile']){
		exi('请填写手机号','error');
	}
	if(strlen($postdata['mobile'])!=11){
		exi('请填写正确的手机号','error');
	}
	if(!$postdata['addressinfo']['lat_lng']){
		exi('请填写正确的坐标','error');
	}
	if(!$postdata['cateid']){
		//exi('请选择商户分类！','error');
	}
	if(!$postdata['uname']){
		exi('请填写用户名','error');
	}
	if(!$postdata['check'] && $setting['applytitle'] && $setting['applycontent']){
		exi('请先阅读协议！','error');
	}
	$postdata['address']=$postdata['addressinfo']['address'];
	$postdata['lat_lng']=$postdata['addressinfo']['lat_lng'];
	$data =array(
		'uname'=>$postdata['uname'],
		'applytime'=>time(),
		'mobile'=>$postdata['mobile'],
		'realname'=>$postdata['realname'],
		'merchname'=>$postdata['merchname'],
		'desc'=>$postdata['desc'],
		'salecate'=>$postdata['salecate'],
		'cateid'=>$postdata['cateid'],
		'status'=>$setting['isshenhe']?0:1,		
		'jointime'=>time(),
		'address'=>$postdata['address'],
		'lat_lng'=>ijson_encode($postdata['lat_lng']),
		'codeimg'=>ijson_encode($postdata['codeimg']),		
		'zhaoshangid'=>$postdata['zhaoshangid'],
		'diyformdata'=>ijson_encode($postdata['diyfields']),
	);
	$ischeckauth=$this->model('model')->checkauth($postdata['id'],true);
	if(!$ischeckauth){
		$data['status']=3;
	}
	if($postdata['upass']){
		$data['upass']=base64_encode($postdata['upass']);
	}
	if($postdata['id']){
		pdo_update('jy_merch_user',$data,array('id'=>$postdata['id'],'mid'=>$_SESSION['uid']));
	}else{
		$log=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'uname'=>$postdata['uname']));
		if($log){
			exi('用户名已被注册','error');
		}
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		pdo_insert('jy_merch_user',$data);
	}
	if($_SESSION['current_plugin'] && $_SESSION['current_plugin']!='core'){
		$url=$_SESSION['current_plugin'].'/index/';
	}else{
		$url='jy_merchfuwu/index/';
	}
	if($postdata['issendsubmsg']){
		$res=array(
			'url'=>$url,
		);
		exi($res);
	}else{
		if($setting['isshenhe']){
			exi('已申请,请等待审核','',$url);
		}else{
			$url='jy_merchfuwu/manage.index/';
			exi('入驻成功','',$url);
		}
	}
	
}
?>